/**
 *
 * @param name cookie名
 * @param cookie cookie值
 * @param time 保存时间(天)
 */
export const setCookie = (name: string, cookie: object, time: number = 30) => {
  document.cookie =
    name +
    "=" +
    JSON.stringify(cookie) +
    "; expires=" +
    new Date(new Date().getTime() + time * 24 * 60 * 60 * 1000).toUTCString();
};

/**
 *
 * @param name cookie名
 * @returns objeck | null
 */
export const getCookie = (name: string) => {
  const userInfoCookie = document.cookie
    .split(";")
    .find((cookie) => cookie.trim().startsWith(`${name}=`))
    ?.split("=")[1];
  return (userInfoCookie ? JSON.parse(userInfoCookie) : undefined) as
    | object
    | undefined;
};
